package it.unibo.convoycruisecontrol.chiefboatsubsystem.test.unit;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import it.unibo.convoycruisecontrol.boatsubsystem.test.mock.MockLocation;
import it.unibo.convoycruisecontrol.boatsubsystem.test.mock.MockLocationDevice;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.DistanceSupervisor;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.test.mock.MockControlPanel;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.test.mock.MockDistanceSupervisor;
import it.unibo.location.ILocation;
import it.unibo.util.information.PrologInformationConverter;

import org.junit.BeforeClass;
import org.junit.Test;

public class DistanceSupervisorTestCase 
{
	static MockDistanceSupervisor distanceSupervisor;
	static int distanceLimit = 50;
	static MockControlPanel controlPanel;


	@BeforeClass
	public static void beforeClass()
	{
		try
		{
			controlPanel = new MockControlPanel();
			distanceSupervisor = new MockDistanceSupervisor(distanceLimit, new MockLocationDevice(), controlPanel, new PrologInformationConverter());
		}
		catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}


	@Test
	public void testDistance()
	{
		assertFalse(controlPanel.isStarted());
		controlPanel.startConvoy();
		assertTrue(controlPanel.isStarted());
		distanceSupervisor.setSpeeds(new float[]{10, 10});

		// 1 km
		List<ILocation> locations = new ArrayList<ILocation>();
		locations.add(new MockLocation(35.75, 14.5, 0));
		locations.add(new MockLocation(35.75, 14.508994, 0));
		distanceSupervisor.setLocations(locations.toArray(new ILocation[0]));
		distanceSupervisor.newInformations();
		assertTrue(controlPanel.isStarted());
		locations.clear();

		// 500 km
		locations.add(new MockLocation(40.67, 15.9, 0));
		locations.add(new MockLocation(45, 17.641825, 0));
		distanceSupervisor.setLocations(locations.toArray(new ILocation[0]));
		distanceSupervisor.newInformations();
		assertTrue(controlPanel.isStarted());
		locations.clear();

		// 100 m
		locations.add(new MockLocation(40.67, 15.9, 0));
		locations.add(new MockLocation(40.67, 15.9009000, 0));
		distanceSupervisor.setLocations(locations.toArray(new ILocation[0]));
		distanceSupervisor.newInformations();
		assertTrue(controlPanel.isStarted());
		locations.clear();

		// 10 m
		locations.add(new MockLocation(40.67, 15.9, 0));
		locations.add(new MockLocation(40.67, 15.9000899, 0));
		distanceSupervisor.setLocations(locations.toArray(new ILocation[0]));
		distanceSupervisor.newInformations();
		assertFalse(controlPanel.isStarted());
	}

	@Test(expected=IllegalArgumentException.class)
	public void testCreation() throws Exception 
	{
		new DistanceSupervisor(-6,new MockLocationDevice(), controlPanel, new PrologInformationConverter());
	}

}
